#include "data_utils.h"

void q_sort8(uint8_t numbers[], uint8_t left, uint8_t right) {
	uint8_t pivot, l_hold, r_hold;

	l_hold = left;
	r_hold = right;
	pivot = numbers[left];
	while (left < right) {
		while ((numbers[right] >= pivot) && (left < right))
			right--;
		if (left != right) {
			numbers[left] = numbers[right];
			left++;
		}
		while ((numbers[left] <= pivot) && (left < right))
			left++;
		if (left != right) {
			numbers[right] = numbers[left];
			right--;
		}
	}
	numbers[left] = pivot;
	pivot = left;
	left = l_hold;
	right = r_hold;
	if (left < pivot)
		q_sort8(numbers, left, pivot - 1);
	if (right > pivot)
		q_sort8(numbers, pivot + 1, right);
}

void q_sort16(uint16_t numbers[], uint8_t left, uint8_t right) {
	uint16_t pivot;
	uint8_t l_hold, r_hold;

	l_hold = left;
	r_hold = right;
	pivot = numbers[left];
	while (left < right) {
		while ((numbers[right] >= pivot) && (left < right))
			right--;
		if (left != right) {
			numbers[left] = numbers[right];
			left++;
		}
		while ((numbers[left] <= pivot) && (left < right))
			left++;
		if (left != right) {
			numbers[right] = numbers[left];
			right--;
		}
	}
	numbers[left] = pivot;
	pivot = left;
	left = l_hold;
	right = r_hold;
	if (left < pivot)
		q_sort16(numbers, left, pivot - 1);
	if (right > pivot)
		q_sort16(numbers, pivot + 1, right);
}
